<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	public function index()
	{
		$session_id = $this->session->userdata('username');
				
			if($session_id){
				$this->load->view('homepage','refresh');
			}else{
			
				$this->load->view('home','refresh');
		
		}
	}
	
	public function loginHandler()
	{
	
		$this->load->database();
	
		$username = $_POST['username'];
		$password = md5($_POST['password']);
			
		$query = $this->db->get_where('login', array('username' => $username));
		
		$user = $query->row();
		if($user->username == $username){
			if($user->password == $password){
	
				$sesh_vals = array(
				 'username'  => $username,
				 'logged_in' => TRUE
				 );

				$this->session->set_userdata($sesh_vals);
				$session_id = $this->session->userdata('username');
				
					redirect('homepage','refresh');
				
			}else{
			
				echo "Wrong Password";
				redirect('home','refresh');
			}
		}else{
			echo "Wrong Username";
			redirect('home','refresh');
		}
	
	}
	
	public function logoutHandler(){
			$this->session->sess_destroy();
			$this->load->view('home','refresh');
	}
	
	public function createUser(){
		$this->load->model('CreateModel');
		$data['create'] = $this->CreateModel->insert_data(); 
	}
}
	
